package cj.hrm.service.impl;

import cj.hrm.dao.*;
import cj.hrm.dto.EmployeeDto;
import cj.hrm.entity.*;
import cj.hrm.service.HrmService;
import cj.hrm.util.page.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人事管理系统服务层接口实现类
 */
@Transactional(propagation= Propagation.REQUIRED,isolation= Isolation.DEFAULT)
@Service("HrmService")
public class HrmServiceImpl implements HrmService {
    /**
     *自动注入持久层Dao对象
     */
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private JobDao jobDao;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private CheckworkDao checkworkDao;
    @Autowired
    private NoticeDao noticeDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private SexDao  sexDao;
    @Autowired
    private StatusDao  statusDao;
    @Autowired
    private EducationDao educationDao;
    @Autowired
    private  RecruitmentDao recruitmentDao;
    @Autowired
    private ResumeDao resumeDao;
    @Autowired
    private JobTypeDao jobTypeDao;
    @Autowired
    private RecruitmentStatusDao recruitmentStatusDao;
    @Autowired
    private PunchClockDao punchClockDao;
    @Autowired
    private PunchTimeDao punchTimeDao;
    @Autowired
    private DocumentDao documentDao;
    /**************用户服务接口实现*************/
    @Override
    public User login(String loginname, String password) {
        User user = userDao.get_login(loginname,password);
        return user;
    }

    @Override
    public User findUserByLoginAndName(String loginname, String username) {
        return userDao.selectByLoginAndName(loginname, username);
    }

    @Override
    public User findUserByLogin(String loginname) {
        return userDao.selectByLogin(loginname);
    }

    @Override
    public User findUserByEmail(String email) {
        return userDao.get_UserByEmail(email);
    }

    @Override
    public User findUserByName(String username) {
        return userDao.selectByName(username);
    }

    @Override
    public User findUserById(Integer id) {
        return userDao.selectById(id);
    }

    @Override
    public void removeUserById(Integer id) {
        userDao.deleteUserById(id);
    }

    @Override
    public void modifyUser(User user) {
        userDao.update_User(user);
    }

    @Override
    public void addUser(User user) {
        userDao.insert_User(user);
    }

    @Override
    public List<User> findUser(User user, PageModel pageModel) {
        /** 当前需要分页的总数据条数  */
        Map<String,Object> params = new HashMap<>();
        params.put("user", user);

        int recordCount =userDao.count(params);
        pageModel.setRecordCount(recordCount);

        if(recordCount > 0){
            /** 开始分页查询数据：查询第几页的数据 */
            params.put("pageModel", pageModel);
        }
        List<User> checkworks = userDao.selectByPage(params);
        return checkworks;
    }

    @Override
    public void toUpdatePassword(String loginname, String password) {
        userDao.updatePasswordByLoginname(loginname,password);
    }

    @Override
    public List<User> get_UserList(User user, PageModel pageModel) {
        /** 当前需要分页的总数据条数  */
        Map<String,Object> params = new HashMap<>();
        params.put("user", user);

        int recordCount =userDao.count(params);
        pageModel.setRecordCount(recordCount);

        if(recordCount > 0){
            /** 开始分页查询数据：查询第几页的数据 */
            params.put("pageModel", pageModel);
        }
        List<User> checkworks = userDao.selectByPage(params);
        return checkworks;
    }

    @Override
    public List<User> get_UserLikeList(String content) {
        return userDao.get_LikeList(content);
    }

    @Override
    public List<User> get_UserEmail() {
        return userDao.get_UserAllEmail();
    }

    @Override
    public Integer countUser(String content) {
        return userDao.countUser(content);
    }

    @Override
    public Integer getEmpIdByUserId(Integer id) {
        return userDao.get_EmpId(id);
    }

    @Override
    public List<Dept> findAllDept() {
        return deptDao.selectAllDept();
    }

    @Override
    public List<Dept> findDept(Dept dept, PageModel pageModel) {
        /** 当前需要分页的总数据条数  */
        Map<String,Object> params = new HashMap<>();
        params.put("dept", dept);
        int recordCount = deptDao.count(params);
        pageModel.setRecordCount(recordCount);

        if(recordCount > 0){
            /** 开始分页查询数据：查询第几页的数据 */
            params.put("pageModel", pageModel);
        }

        List<Dept> depts = deptDao.selectByPage(params);

        return depts;
    }

    @Override
    public List<Dept> findAllDeptByContent(String content) {
        return deptDao.selectLikeAllDept(content);
    }

    @Override
    public void addDept(Dept dept) {
        deptDao.insert_Dept(dept);
    }

    @Override
    public Dept findDeptByName(String name) {
        return deptDao.getDeptByName(name);
    }

    @Override
    public Dept findDeptById(Integer id) {
        Dept dept = deptDao.selectById(id);
        return dept;
    }

    @Override
    public void modifyDept(Dept dept) {
        deptDao.update_Dept(dept);
    }

    @Override
    public void removeDeptById(Integer id) {
        deptDao.deleteDeptById(id);
    }

    @Override
    public List<Job> findAllJob() {
        return jobDao.selectAllJob();
    }

    @Override
    public List<Job> findAllJob(String content) {
        return jobDao.get_LikeList(content);
    }
    @Override
    public List<Job> findJob(Job job, PageModel pageModel) {
        /** 当前需要分页的总数据条数  */
        Map<String,Object> params = new HashMap<>();
        params.put("job", job);
        int recordCount = jobDao.count(params);
        pageModel.setRecordCount(recordCount);

        if(recordCount > 0){
            /** 开始分页查询数据：查询第几页的数据 */
            params.put("pageModel", pageModel);
        }

        List<Job> jobs = jobDao.selectByPage(params);

        return jobs;
    }
    @Override
    public Job findJobById(Integer id) {
        return jobDao.selectById(id);
    }

    @Override
    public Job findJobByName(String name) {
        return jobDao.selectJobByName(name);
    }

    @Override
    public void addJob(Job job) {
        jobDao.insert_Job(job);
    }

    @Override
    public void removeJobById(Integer id) {
        jobDao.deleteJobById(id);
    }

    @Override
    public void modifyJob(Job job) {
        jobDao.update_Job(job);
    }

    @Override
    public List<Employee> findEmployee(Employee employee, PageModel pageModel) {
        /** 当前需要分页的总数据条数  */
        Map<String,Object> params = new HashMap<>();
        params.put("employee", employee);

        int recordCount =employeeDao.count(params);
        pageModel.setRecordCount(recordCount);

        if(recordCount > 0){
            /** 开始分页查询数据：查询第几页的数据 */
            params.put("pageModel", pageModel);
        }
        List<Employee> employees = employeeDao.selectByPage(params);
        return employees;
    }

    @Override
    public List<Employee> get_EmployeeLikeList(String content) {
        return employeeDao.get_LikeList(content);
    }

    @Override
    public Integer countEmployee(String content) {
        return employeeDao.countEmployee(content);
    }

    @Override
    public Integer findEmployeeByName(String employee_name) {
        return employeeDao.getIdByName(employee_name);
    }

    @Override
    public List<EmployeeDto> findEmployeeDtoList(){
        List<EmployeeDto> employees = employeeDao.findEmployeeDtoList();
        return employees;
    }

    @Override
    public Employee findEmployeeById(Integer id) {
        return employeeDao.get_Info(id);
    }

    @Override
    public void modifyEmployee(Employee employee) {
        employeeDao.update_Employee(employee);
    }

    @Override
    public void addEmployee(Employee employee) {
        employeeDao.insert_Employee(employee);
    }

    @Override
    public void removeEmployeeById(Integer id) {
        employeeDao.deleteEmployeeById(id);
    }

    @Override
    public List<Checkwork> findCheckwork(Checkwork checkwork, PageModel pageModel) {
        /** 当前需要分页的总数据条数  */
        Map<String,Object> params = new HashMap<>();
        params.put("checkwork", checkwork);

        int recordCount =checkworkDao.count(params);
        pageModel.setRecordCount(recordCount);

        if(recordCount > 0){
            /** 开始分页查询数据：查询第几页的数据 */
            params.put("pageModel", pageModel);
        }
        List<Checkwork> checkworks = checkworkDao.selectByPage(params);
        return checkworks;
    }

    @Override
    public List<Checkwork> findCheckworkLikeList(String content) {
        return checkworkDao.get_LikeList(content);
    }

    @Override
    public Integer countCheckwork(String content) {
        return checkworkDao.countCheckwork(content);
    }

    @Override
    public Checkwork findCheckworkByEmp_id(Integer id) {
        return checkworkDao.getEmp_id(id);
    }

    @Override
    public Checkwork findCheckworkById(Integer id) {
        return checkworkDao.selectById(id);
    }

    @Override
    public void modifyCheckwork(Checkwork checkwork) {
        checkworkDao.update_Checkwork(checkwork);
    }

    @Override
    public void addCheckwork(Checkwork checkwork) {
        checkworkDao.insert_CheckWork(checkwork);
    }

    @Override
    public void removeCheckworkById(Integer id) {
        checkworkDao.deleteCheckworkById(id);
    }

    @Override
    public List<Notice> findNotice(Notice notice, PageModel pageModel) {
        /** 当前需要分页的总数据条数  */
        Map<String,Object> params = new HashMap<>();
        params.put("notice", notice);

        int recordCount =noticeDao.count(params);
        pageModel.setRecordCount(recordCount);

        if(recordCount > 0){
            /** 开始分页查询数据：查询第几页的数据 */
            params.put("pageModel", pageModel);
        }
        List<Notice> checkworks = noticeDao.selectByPage(params);
        return checkworks;
    }

    @Override
    public List<Notice> get_NoticeLikeList(String content) {
        return noticeDao.get_LikeList(content);
    }

    @Override
    public Integer countNotice(String content) {
        return noticeDao.countNotice(content);
    }

    @Override
    public Notice findNoticeById(Integer id) {
        return noticeDao.selectById(id);
    }

    @Override
    public void removeNoticeById(Integer id) {
        noticeDao.deleteNoticeById(id);
    }

    @Override
    public void addNotice(Notice notice) {
        noticeDao.insert_Notice(notice);
    }

    @Override
    public void modifyNotice(Notice notice) {
        noticeDao.update_Notice(notice);
    }

    @Override
    public List<Sex> findAllSex(String content) {
        return sexDao.selectLikeAllSex(content);
    }

    @Override
    public List<Sex> findAllSex() {
        return sexDao.selectAllSex();
    }

    @Override
    public List<Education> findAllEducation(String content) {
        return educationDao.selectLikeAllEducation(content);
    }

    @Override
    public List<Education> findAllEducation() {
        return educationDao.selectAllEducation();
    }

    @Override
    public List<Status> findAllStatus(String content) {
        return statusDao.selectLikeAllStatus(content);
    }

    @Override
    public List<Status> findAllStatus() {
        return statusDao.selectAllStatus();
    }

    @Override
    public Integer findChe_id() {
        return employeeDao.togetChe_id()+1;
    }

    @Override
    public User findUserMessageByLoginname(String loginname) {
        return userDao.selectByLogin(loginname);
    }

    @Override
    public List<Recruitment> findAllRecruitment() {
        List<Recruitment> recruitments = recruitmentDao.selectByPage();

        return recruitments;
    }

    @Override
    public List<Recruitment> get_RecruitmentLikeList(String content) {
        return recruitmentDao.get_LikeList(content);
    }

    @Override
    public List<Recruitment> findRecruitment(Recruitment recruitment, PageModel pageModel) {
        /** 当前需要分页的总数据条数  */
        Map<String,Object> params = new HashMap<>();
        params.put("recruitment", recruitment);

        int recordCount =recruitmentDao.count(params);
        pageModel.setRecordCount(recordCount);

        if(recordCount > 0){
            /** 开始分页查询数据：查询第几页的数据 */
            params.put("pageModel", pageModel);
        }
        List<Recruitment> recruitments = recruitmentDao.selectByPage1(params);
        return recruitments;
    }

    @Override
    public Recruitment findRecruitmentById(Integer id) {
        Recruitment recruitment = recruitmentDao.selectRecruitmentById(id);

        return recruitment;
    }

    @Override
    public void addResume(Resume resume) {
        resumeDao.insert_Resume(resume);
    }

    @Override
    public void removeRecruitmentById(Integer id) {
        recruitmentDao.deleteRecruitmentById(id);
    }

    @Override
    public List<Resume> get_ResumeLikeList(String content) {
        return resumeDao.get_LikeList(content);
    }

    @Override
    public List<Resume> findResume(Resume resume, PageModel pageModel) {
        /** 当前需要分页的总数据条数  */
        Map<String,Object> params = new HashMap<>();
        params.put("resume", resume);

        int recordCount =resumeDao.count(params);
        pageModel.setRecordCount(recordCount);

        if(recordCount > 0){
            /** 开始分页查询数据：查询第几页的数据 */
            params.put("pageModel", pageModel);
        }
        List<Resume> resumes = resumeDao.selectByPage(params);
        return resumes;
    }

    @Override
    public Resume findResumeById(Integer id) {
        return resumeDao.selectById(id);
    }

    @Override
    public void modifyResume(Resume resume) {
        resumeDao.update_Resume(resume);
    }

    @Override
    public Integer countResume(String content) {
        return resumeDao.countResume(content);
    }

    @Override
    public Integer countRecruitment(String content) {
        return recruitmentDao.countRecruitment(content);
    }

    @Override
    public List<JobType> findAllJobType() {
        return jobTypeDao.selectAllJobType();
    }

    @Override
    public List<RecruitmentStatus> findAllRecruitmentStatus() {
        return recruitmentStatusDao.selectAllRecruitmentStatus();
    }

    @Override
    public void addRecruitment(Recruitment recruitment) {
        recruitmentDao.insert_Recruitment(recruitment);
    }

    @Override
    public void modifyRecruitment(Recruitment recruitment) {
        recruitmentDao.update_Recruitment(recruitment);
    }

    @Override
    public PunchClock getPunchClockByDateAndUserId(String date, Integer userID) {
        return punchClockDao.selectPunchClockByDateAndUserId(date, userID);
    }

    @Override
    public void addPunchClock(PunchClock punchClock) {
        punchClockDao.insert_PunchClock(punchClock);
    }

    @Override
    public void modifyPunchClock(Integer id, String date, Date date1, int status) {
        punchClockDao.update_PunchClock(id,date,date1,status);
    }

    @Override
    public List<PunchClock> findAllPunchById(Integer id) {
        return punchClockDao.selectAllPunchById(id);
    }

    @Override
    public void modifyPunchClocks(Integer id, String date, Date date1, int status, int offwork_status_id) {
        punchClockDao.update_PunchClocks(id,date,date1,status,offwork_status_id);
    }

    @Override
    public PunchClock findPunchClockById(Integer id) {
        return punchClockDao.selectPunchClockById(id);
    }

    @Override
    public void modifyPunchClockContent(Integer id, String content) {
        punchClockDao.update_PunchClockContent(id,content);
    }

    @Override
    public List<PunchClock> getPunchClockByUserID(Integer id, String date) {
        return punchClockDao.selectPunchClockByUserID(id,date);
    }

    @Override
    public PunchTime getPt() {
        return punchTimeDao.getPunchTime();
    }

    @Override
    public void updatePt(String am, String pm, Integer id) {
        punchTimeDao.updatePunchTime(am,pm,id);
    }

    /**
     * 文档管理
     */
    @Override
    public List<Document> get_DocumentList(Document document,PageModel pageModel) {
        /** 当前需要分页的总数据条数  */
        Map<String,Object> params = new HashMap<>();
        params.put("document", document);

        int recordCount =documentDao.count(params);
        pageModel.setRecordCount(recordCount);

        if(recordCount > 0){
            /** 开始分页查询数据：查询第几页的数据 */
            params.put("pageModel", pageModel);
        }
        List<Document> checkworks = documentDao.selectByPage(params);
        return checkworks;
    }
    @Override
    public List<Document> get_DocumentLikeList(String content) {
        return documentDao.get_LikeList(content);
    }

    @Override
    public Integer countDocument(String content) {
        return documentDao.countDocument(content);
    }

    @Override
    public Document get_DocumentInfo(Integer id) {
        return documentDao.get_Info(id);
    }
    @Override
    public void update_DocumentInfo(Document notice) {
        documentDao.update_Info(notice);
    }
    @Override
    public void insert_DocumentInfo(Document notice) {
        documentDao.insert_Info(notice);
    }
    @Override
    public void delete_DocumentInfo(Integer id) {
        documentDao.delete_Info(id);
    }

}
